package com.holyn.guofang.view.my;

import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.DeviceWo;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.my.MyContract;
import com.holyn.guofang.presenter.my.MyPresenter;
import com.holyn.guofang.view.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/8/18.
 */

public class MyRegisterInfoSecondaryDeviceListActivity extends BaseActivity implements BaseView, MyContract.SecondaryDeviceListView{
    private static final int REQUEST_ACTION_POST_DEVICE_LIST = 0x0001;
    private static final int REQUEST_ACTION_POST_UPDATE_DEVICE_REMARK = 0x0002;
    private static final int REQUEST_ACTION_POST_UPDATE_AUTHORIZE = 0x0003;
//    private static final int REQUEST_ACTION_POST_DELETE_DEVICE = 0x0004;
    private static final int REQUEST_ACTION_POST_STOP_DEVICE = 0x0005;

    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.btn_save)
    Button btnSave;
    @BindView(R.id.btn_authorize)
    Button btnAuthorize;
    @BindView(R.id.btn_delete)
    Button btnDelete;

    MyContract.Presenter presenter;

    List<DeviceWo.DataBean> deviceList = new ArrayList<>();
    MyRecyclerAdapter recyclerAdapter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_my_register_secondary_device_list;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("附属号管理");

        presenter = new MyPresenter(this);
        presenter.addSecondaryDeviceListView(this);

        presenter.postDeviceListB(REQUEST_ACTION_POST_DEVICE_LIST);
    }

    @OnClick({R.id.btn_save, R.id.btn_authorize, R.id.btn_delete})
    public void onViewClicked(View view) {
        int curCheckPosition = recyclerAdapter.getCurCheckPosition();
        if (curCheckPosition == -1){
            toastMsg("请先选择设备");
            return;
        }
        switch (view.getId()) {
            case R.id.btn_save:
                presenter.postUpdateDeviceRemarkB(REQUEST_ACTION_POST_UPDATE_DEVICE_REMARK, deviceList.get(curCheckPosition).getId(), recyclerAdapter.getCurCheckRemark());
                break;
            case R.id.btn_authorize:
                presenter.postUpdateAuthorizeB(REQUEST_ACTION_POST_UPDATE_AUTHORIZE, deviceList.get(curCheckPosition).getId());
                break;
            case R.id.btn_delete:
                presenter.postUpdateAuthorizeStop(REQUEST_ACTION_POST_STOP_DEVICE, deviceList.get(curCheckPosition).getId());
                break;
        }
    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        toastMsg("出现异常");
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(Integer requestAction, StatusWo statusWo) {
        if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            toastMsg("操作成功");
            presenter.postDeviceListB(REQUEST_ACTION_POST_DEVICE_LIST);
        } else {
            toastMsg("操作失败:"+statusWo.getErr());
        }
    }

    @Override
    public void responseSuccess(Integer requestAction, DeviceWo deviceWo) {
        if (deviceWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            deviceList = deviceWo.getData();
            initRecyclerView();
        } else {
            toastMsg("获取设备列表失败！");
        }
    }

    private void initRecyclerView(){
        recyclerAdapter = new MyRecyclerAdapter(deviceList);
        recyclerAdapter.bindToRecyclerView(rvList);
        rvList.setLayoutManager(new LinearLayoutManager(MyRegisterInfoSecondaryDeviceListActivity.this));
        rvList.setAdapter(recyclerAdapter);
    }

    private class MyRecyclerAdapter extends BaseQuickAdapter<DeviceWo.DataBean, BaseViewHolder>{

        private List<Boolean> isCheckList = new ArrayList<>();

        public MyRecyclerAdapter(@Nullable List<DeviceWo.DataBean> data) {
            super(R.layout.item_my_register_secondary_device_list, data);
            for (int i = 0; i<data.size(); i++){
                isCheckList.add(false);
            }
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, DeviceWo.DataBean dataBean) {
            boolean isCheck = isCheckList.get(baseViewHolder.getAdapterPosition());

            baseViewHolder.setText(R.id.tv_num, dataBean.getEquipmentCode());
            ImageView ivSelect = baseViewHolder.getView(R.id.iv_select);
            EditText etName = baseViewHolder.getView(R.id.et_name);
            if (isCheck){
                ivSelect.setImageResource(R.drawable.ic_check_select);
                etName.setEnabled(true);
                etName.setFocusable(true);
            } else {
                ivSelect.setImageResource(R.drawable.ic_check_normal);
                etName.setEnabled(false);
                etName.setFocusable(false);
            }

            ivSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    isCheckList.set(baseViewHolder.getAdapterPosition(), !isCheck);
                    for (int i=0; i<isCheckList.size(); i++){
                        if (i != baseViewHolder.getAdapterPosition()){
                            isCheckList.set(i, false);
                        }
                    }
                    notifyDataSetChanged();
                }
            });

            if (dataBean.isIsAuthorize()){
                baseViewHolder.setText(R.id.tv_status, "已授权");
            } else {
                baseViewHolder.setText(R.id.tv_status, "未授权");
            }
            if (TextUtils.isEmpty(dataBean.getRemark())){
                etName.setText("设备-"+dataBean.getId());
            } else {
                etName.setText(dataBean.getRemark());
            }

        }

        public int getCurCheckPosition() {
            for (int i = 0; i < isCheckList.size(); i++){
                if (isCheckList.get(i)){
                    return i;
                }
            }
            return -1;
        }

        public String getCurCheckRemark() {
            EditText etRemark = (EditText) getViewByPosition(getCurCheckPosition(), R.id.et_name);
            return etRemark.getText().toString().trim();
        }
    }
}
